<?php

/**
 * uniBoard Style edit
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_style{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Style API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Style Copy API
	 *
	 * @var object
	 */
	
	private $copy_api		= NULL;
	
	/**
	 * Styles list (our little cache)
	 *
	 * @var array
	 */
	
	private $styles			= array();
	
	/**
	 * Imagesets list
	 *
	 * @var array
	 */
	
	private $imagesets		= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Start Style API
		$this -> api = new api_style( $appCore, $appCore -> _REQ( 'style'));
		
		// Did we loaded api from DB?
		if ( $this -> api -> error || !$this -> api -> data_loaded)
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['acp_style_not_found'], $appCore -> lang['acp_styles_list']);
			
			// Trow user back to groups list
			new action_styles( $appCore);	
		}
		else if ( $this -> api -> style_id == 1 && !DEVELOPER_MODE)
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['acp_style_master_edit'], $appCore -> lang['acp_styles_list']);
			
			// Trow user back to groups list
			new action_styles( $appCore);	
		}
		else if ( $this -> api -> style_id == 1 && !( $appCore -> _REQ( 'do') == 'details' || $appCore -> _REQ( 'do') == 'css' || $appCore -> _REQ( 'do') == 'js'))
		{
			// Draw error
			$appCore -> output -> error( $appCore -> lang['acp_style_master_not_allowed'], $appCore -> lang['acp_styles_list']);
			
			// Trow user back to groups list
			new action_styles( $appCore);
		}
		else
		{
			// Handle action
			if ( $appCore -> _REQ( 'do') == 'details')
			{
				// Collect imagesets
				$appCore -> db -> query( 'SELECT imageset_id, imageset_name
				FROM ' . DB_PREFIX . 'imagesets
				ORDER BY imageset_name');
				
				// Parse result
				while ( $imageset_result = $appCore -> db -> fetch_array())
				{
					// Add imageset to cache store
					$this -> imagesets[$imageset_result[0]] = htmlspecialchars( $imageset_result[1]);
				}
				
				// Free result
				$appCore -> db -> freeResult();
		
				// Update style?
				if ( $appCore -> _POST( 'update') == 1)
				{
					// Fill API
					$this -> api -> setStyleName( $appCore -> _POST( 'style_name'));
					$this -> api -> setStyleAuthor( $appCore -> _POST( 'style_author'));
					$this -> api -> setStyleEmail( $appCore -> _POST( 'style_email'));
					$this -> api -> setStyleWww( $appCore -> _POST( 'style_www'));
					
					// Not master style
					if ( $this -> api -> style_id > 1)
					{
						// Set Parent
						$this -> api -> setStyleParent( $appCore -> _POST( 'style_parent'));
						
						// Set new imageset
						if ( isset( $this -> imagesets[ $appCore -> _POST( 'style_imageset')]))
						{
							$this -> api -> setImagesetId( $appCore -> _POST( 'style_imageset'));	
						}
						else
						{
							$this -> api -> setError( $appCore -> lang['acp_imageset_not_found']);
						}
					}
										
					// Error?
					if ( $this -> api -> error)
					{
						// Draw error messages
						$appCore -> output -> error( join( '<br />', $this -> api -> errors), htmlspecialchars( $this -> api -> style_name));
									
						// Repeat form
						$this -> drawDetailsForm();
					}
					else
					{
						// Save changes
						$this -> api -> saveData();
						
						// Draw message
						$appCore -> output -> message( $appCore -> lang['acp_style_details_updated'], htmlspecialchars( $this -> api -> style_name));
						
						// Set as default?
						if ( $this -> api -> style_id > 1 && $this -> api -> style_id != $appCore -> conf['default_style'] && $appCore -> _POST( 'style_make_default') == 1)
						{
							$appCore -> setConfig( 'default_style', $this -> api -> style_id);	
						}
						
						// Create Log
						$appCore -> logAdmin( 'log_admin_changed_style_details', htmlspecialchars( $this -> api -> style_name));
			
						// Display styles list
						new action_styles( $appCore);
					}
				}
				else
				{	
					// Draw details form
					$this -> drawDetailsForm();
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'css')
			{
				// Update style?
				if ( $appCore -> _POST( 'update') == 1)
				{
					// Update style
					$this -> api -> setStyleCss( $appCore -> _POST( 'style_css'));
					
					// Error?
					if ( $this -> api -> error)
					{
						// Show error
						$appCore -> output -> error( $this -> api -> last_error, htmlspecialchars( $this -> api -> style_name));
						
						// Draw form
						$this -> drawCssForm();
					}
					else
					{
						// Update api
						$this -> api -> saveData();
						
						// If we are not in master, update cache
						if ( $this -> api -> style_id > 1)
						{
							file_put_contents( STYLES_DATA_PATH . 'style_' . $this -> api -> style_id . '.css', class_cache::makeFileStamp( 'STYLE ' . $this -> api -> style_id . ' CSS FILE', false) . str_ireplace( array( '{IMAGES_DIR}', '{BLUEPRINT_DIR}'), array( 'imageset_' . $this -> api -> imageset_id, CSS_FRAMEWORK_PATH), $this -> api -> style_css));
						}
						
						// Create Log
						$appCore -> logAdmin( 'log_admin_changed_style_css', htmlspecialchars( $this -> api -> style_name));
			
						// Show message
						$appCore -> output -> message( $appCore -> lang['acp_style_css_updated'], $appCore -> lang['style_op_css']);
						
						// Get back to list?
						if ( strlen( $appCore -> _POST( 'leave_editor')) > 0)
						{
							// Styles list
							new action_styles( $appCore);
						}
						else
						{
							// Draw form
							$this -> drawCssForm();
						}
					}
				}
				else
				{
					// Draw CSS form
					$this -> drawCssForm();
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'js')
			{
				// Update style?
				if ( $appCore -> _POST( 'update') == 1)
				{
					// Update javascript
					$this -> api -> setStyleJs( $appCore -> _POST( 'style_js'));
					
					// Update api
					$this -> api -> saveData();
					
					// If we are not in master, update cache
					if ( $this -> api -> style_id > 1)
					{
						file_put_contents( STYLES_DATA_PATH . 'jscript_' . $this -> api -> style_id . '.js', class_cache::makeFileStamp( 'STYLE ' . $this -> api -> style_id . ' JAVASCRIPT FILE', false) . $this -> api -> style_js);
					}
					
					// Create Log
					$appCore -> logAdmin( 'log_admin_changed_style_js', htmlspecialchars( $this -> api -> style_name));
			
					// Show message
					$appCore -> output -> message( $appCore -> lang['acp_style_js_updated'], $appCore -> lang['style_op_js']);
					
					// Get back to list?
					if ( strlen( $appCore -> _POST( 'leave_editor')) > 0)
					{
						// Styles list
						new action_styles( $appCore);
					}
					else
					{
						// Draw form
						$this -> drawJsForm();
					}
				}
				else
				{
					// Draw CSS form
					$this -> drawJsForm();
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'resynch')
			{
				// Master style?
				if ( $this -> api -> style_id == 1)
				{
					// Draw error
					$appCore -> output -> error( $appCore -> lang['acp_style_master_not_allowed'], $appCore -> lang['acp_styles_list']);
					
					// Trow user back to groups list
					new action_styles( $appCore);
				}
				else
				{
					// Run resynchroniser
					$this -> resynchStyle( $this -> api -> style_id);
					
					// Create Log
					$appCore -> logAdmin( 'log_admin_compiled_templates', htmlspecialchars( $this -> api -> style_name));
			
					// Redirect us back to styles list
					$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=styles'), $appCore -> lang['acp_style_resynchronised']);
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'users')
			{
				// Make cache
				$this -> styles = $appCore -> getStyles( false);
				
				// Change users?
				if ( $appCore -> _POST( 'update') == 1)
				{
					// Grab new style
					$new_style = (integer) $appCore -> _POST( 'style_replace');
					
					// Style not in array?
					if ( !isset( $this -> styles[$new_style]))
					{
						// Show error
						$appCore -> output -> error( $appCore -> lang['acp_style_not_found'], htmlspecialchars( $this -> api -> style_name));
						
						// Draw form
						$this -> drawUsersForm();
					}
					else if ( $new_style == $this -> api -> style_id)
					{
						// Show error
						$appCore -> output -> error( $appCore -> lang['acp_style_new_style_same'], htmlspecialchars( $this -> api -> style_name));
						
						// Draw form
						$this -> drawUsersForm();
					}
					else
					{
						// Reassign users
						$appCore -> db -> update( array( 'user_style' => $new_style), 'users', 'user_style = \'' . $this -> api -> style_id . '\' AND user_id > \'0\'');
						
						// Create Log
						$appCore -> logAdmin( 'log_admin_changed_style_users', htmlspecialchars( $this -> api -> style_name));
			
						// Redirect user to styles list
						$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=styles'), $appCore -> lang['acp_style_users_updated']);		
					}
				}
				else
				{
					// Draw form
					$this -> drawUsersForm();
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'copy')
			{
				// Create api copy
				$this -> copy_api = clone $this -> api;
				
				// Fix id's
				$this -> copy_api -> style_id = 0;
				$this -> copy_api -> setStyleParent( $this -> api -> style_id);
				
				// Submit form?
				if ( $appCore -> _POST( 'update') == 1)
				{
					// Fill api name things
					$this -> copy_api -> setStyleName( $appCore -> _POST( 'style_name'));
					$this -> copy_api -> setStyleAuthor( $this -> api -> style_author);
					$this -> copy_api -> setStyleEmail( $this -> api -> style_email);
					$this -> copy_api -> setStyleWww( $this -> api -> style_www);
					$this -> copy_api -> setImagesetId( $this -> api -> imageset_id);
					$this -> copy_api -> setStyleCss( $this -> api -> style_css);
					$this -> copy_api -> setStyleJs( $this -> api -> style_js);
					
					// Api errors?
					if ( $this -> copy_api -> error)
					{
						// Draw error
						$appCore -> output -> error( $this -> copy_api -> last_error, $appCore -> lang['style_op_make_chidren']);
						
						// Repeat form
						$this -> drawCopyForm();
					}
					else
					{
						// Save api
						$this -> copy_api -> saveData();
												
						// Create CSS and JS files
						file_put_contents( STYLES_DATA_PATH . 'style_' . $this -> copy_api -> style_id . '.css', class_cache::makeFileStamp( 'STYLE ' . $this -> copy_api -> style_id . ' CSS FILE', false) . str_ireplace( array( '{IMAGES_DIR}', '{BLUEPRINT_DIR}'), array( 'imageset_' . $this -> copy_api -> imageset_id, CSS_FRAMEWORK_PATH), $this -> copy_api -> style_css));
						file_put_contents( STYLES_DATA_PATH . 'jscript_' . $this -> copy_api -> style_id . '.js', class_cache::makeFileStamp( 'STYLE ' . $this -> copy_api -> style_id . ' JAVASCRIPT FILE', false) . $this -> copy_api -> style_js);
					
						// Create new templates dir
						class_files::newDir( STYLES_DATA_PATH . 'style_' . $this -> copy_api -> style_id);
						
						// Resynch templates
						$this -> resynchStyle( $this -> copy_api -> style_id);
						
						// Create Log
						$appCore -> logAdmin( 'log_admin_copied_style', htmlspecialchars( $this -> api -> style_name));
			
						// Redirect us to new page
						$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=styles'), $appCore -> lang['acp_style_child_saved']);
					}
				}
				else
				{
					// Display copy form
					$this -> drawCopyForm();	
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'export')
			{
				// Make export?
				if ( $appCore -> _POST( 'export') == 1)
				{	
					// Create Log
					$appCore -> logAdmin( 'log_admin_exported_style', htmlspecialchars( $this -> api -> style_name));
			
					// Perform export
					$this -> makeExport();
				}
				else
				{
					// Draw export form
					$this ->drawExportForm();
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'delete')
			{
				// Allow style delete?
				if ( $this -> api -> style_id == $appCore -> conf['default_style'])
				{
					// Draw error
					$appCore -> output -> error( $appCore -> lang['acp_style_default_delete'], $appCore -> lang['style_op_delete']);
					
					// Trow user back to styles list
					new action_styles( $appCore);
				}
				else
				{
					// Start building "move to" list
					$this -> styles = array( 0 => $appCore -> lang['style_form_move_children_0']);
					
					//  Add styles list
					$this -> styles = $this -> styles + $appCore -> getStyles( true, $this -> api -> style_id);
					
					// Delete style?
					if ( $appCore -> _POST( 'delete') == 1)
					{
						// User's pick
						$choice = (int) $appCore -> _POST( 'style_replace');
						
						// Choice correct?
						if ( !isset( $this -> styles[$choice]))
						{
							// Draw error
							$appCore -> output -> error( $appCore -> lang['acp_style_new_parent_wrong'], $appCore -> lang['style_op_delete']);
							
							// Repeat form
							$this -> drawDeleteForm();
						}
						else
						{
							// Delete style
							$this -> deleteStyle( $choice);
							
							// Create Log
							$appCore -> logAdmin( 'log_admin_deleted_style', htmlspecialchars( $this -> api -> style_name));
						}
					}
					else
					{
						// Draw Delete Form
						$this -> drawDeleteForm();
					}
				}
			}
			else
			{
				// Draw last error
				$appCore -> output -> error( $appCore -> lang['wrong_action_code'], $appCore -> lang['acp_styles_list']);
				
				// Trow user back to groups list
				new action_styles( $appCore);
			}
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws details form
	 *
	 */
	
	function drawDetailsForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_style&do=details&style=' . $this -> api -> style_id));
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['style_form_sect_basic']);

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['style_form_name'], $this -> appCore -> drawTextInput( 'style_name', $this -> api -> style_name));
		
		// Not master style
		if ( $this -> api -> style_id > 1)
		{
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['style_form_parent'], $this -> appCore -> drawList( 'style_parent', $this -> appCore -> getStyles( true, $this -> api -> style_id), $this -> api -> style_parent));
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['style_form_set_as_default'], ( $this -> api -> style_id == $this -> appCore -> conf['default_style'] ? $this -> appCore -> lang['style_form_style_default'] : $this -> appCore -> drawYesNo( 'style_make_default', $this -> appCore -> _POST( 'style_make_default'))));
		
			// Imageset block
			echo $this -> appCore -> output -> closeFormBlock();
			echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['style_form_sect_imageset']);
			
			// Draw list of imagesets
			echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['style_form_imageset'], $this -> appCore -> drawList( 'style_imageset', $this -> imagesets, $this -> api -> imageset_id));
		}
			
		// Author block
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['style_form_sect_author']);

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['style_form_author_name'], $this -> appCore -> drawTextInput( 'style_author', $this -> api -> style_author));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['style_form_author_email'], $this -> appCore -> drawTextInput( 'style_email', $this -> api -> style_email));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['style_form_author_www'], $this -> appCore -> drawTextInput( 'style_www', $this -> api -> style_www));
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['style_op_edit'], array(
			$this -> appCore -> adminLink( 'act=styles') => $this -> appCore -> lang['acp_styles_list'],
			'' => htmlspecialchars( $this -> api -> style_name)
		), ob_get_clean());
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws CSS edition form
	 *
	 */
	
	function drawCssForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_style&do=css&style=' . $this -> api -> style_id), 'editor_form');
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock();

		// Draw text-area
		echo $this -> appCore -> output -> tableRow( $this -> appCore -> drawBigTextArea( 'style_css', $this -> api -> style_css));
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes'], $this -> appCore -> drawButton( 'leave_editor', $this -> appCore -> lang['save_and_leave']));
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['style_op_css'], array(
			$this -> appCore -> adminLink( 'act=styles') => $this -> appCore -> lang['acp_styles_list'],
			'' => htmlspecialchars( $this -> api -> style_name)
		), ob_get_clean());
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws JavaScript edition form
	 *
	 */
	
	function drawJsForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_style&do=js&style=' . $this -> api -> style_id), 'editor_form');
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock();

		// Draw text-area
		echo $this -> appCore -> output -> tableRow( $this -> appCore -> drawBigTextArea( 'style_js', $this -> api -> style_css));
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes'], $this -> appCore -> drawButton( 'leave_editor', $this -> appCore -> lang['save_and_leave']));
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['style_op_js'], array(
			$this -> appCore -> adminLink( 'act=styles') => $this -> appCore -> lang['acp_styles_list'],
			'' => htmlspecialchars( $this -> api -> style_name)
		), ob_get_clean());
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Performs compilation of style templates
	 *
	 * @param integer $style_id
	 */
	
	function resynchStyle( $style_id){
		
		// If style directory doesnt exists, create it
		if ( !file_exists( STYLES_DATA_PATH . 'style_' . $style_id . '/index.html'))
		{
			class_files::newDir( STYLES_DATA_PATH . 'style_' . $style_id);
		}
		
		// Get style parents
		$style_parents = $this -> appCore -> getStyleParents( $style_id);
		
		// Select Templates Groups from DB
		$this -> appCore -> db -> query( 'SELECT DISTINCT template_group
		FROM ' . DB_PREFIX . 'styles_templates
		WHERE style_id IN (' . join( ',', $style_parents) . ')');
		
		// Compile Template Groups
		while ( $group_result = $this -> appCore -> db -> fetch_array())
		{
			// Create Compiler
			$tpl_compiler = new class_template_compiler( $this -> appCore, $style_id, $style_parents, $group_result[0]);
			
			// Compile template to specified location
			$tpl_compiler -> compile( STYLES_DATA_PATH . 'style_' . $style_id . '/tpl_' . $this -> appCore -> secureFileName( $group_result[0]). PHP_EXT);	
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Users change style form
	 *
	 */
	
	function drawUsersForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Set page help
		$this -> appCore -> output -> page_help = $this -> appCore -> lang['acp_style_users_help'];
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_style&do=users&style=' . $this -> api -> style_id), 'editor_form');
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock();

		// Draw style select
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['style_form_change_style'], $this -> appCore -> drawList( 'style_replace', $this -> styles, $this -> api -> style_id));
			
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['run_tool']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['style_op_users'], array(
			$this -> appCore -> adminLink( 'act=styles') => $this -> appCore -> lang['acp_styles_list'],
			'' => htmlspecialchars( $this -> api -> style_name)
		), ob_get_clean());
	
	}	
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Style Copy Form (which actually makes style children, not copy lol)
	 *
	 */
	
	function drawCopyForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_style&do=copy&style=' . $this -> api -> style_id));
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['style_form_sect_basic']);

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['new_style_form_name'], $this -> appCore -> drawTextInput( 'style_name', $this -> copy_api -> style_name), $this -> appCore -> lang['new_style_form_children_expl']);
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['style_op_make_chidren'], array(
			$this -> appCore -> adminLink( 'act=styles') => $this -> appCore -> lang['acp_styles_list'],
			'' => htmlspecialchars( $this -> api -> style_name)
		), ob_get_clean());
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Export Form
	 *
	 */
	
	function drawExportForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_style&do=export&style=' . $this -> api -> style_id));
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'export', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock();

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['style_form_export_templates'], $this -> appCore -> drawYesNo( 'style_export_all'), $this -> appCore -> lang['style_form_export_templates_expl']);
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['export_data']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['style_op_export'], array(
			$this -> appCore -> adminLink( 'act=styles') => $this -> appCore -> lang['acp_styles_list'],
			'' => htmlspecialchars( $this -> api -> style_name)
		), ob_get_clean());
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Performs Style export
	 *
	 */
	
	function makeExport(){
		
		// Start content
		$file_content = '<?xml version="1.0" encoding="UTF-8"?>
<uniboard_style_export>
	<style_data>
		<style_name>' . base64_encode( $this -> api -> style_name) .'</style_name>
		<style_author>' . base64_encode( $this -> api -> style_author) .'</style_author>
		<style_email>' . base64_encode( $this -> api -> style_email) .'</style_email>
		<style_www>' . base64_encode( $this -> api -> style_www) .'</style_www>
		<style_css>' . base64_encode( $this -> api -> style_css) .'</style_css>
		<style_js>' . base64_encode( $this -> api -> style_js) .'</style_js>
	</style_data>
	<style_templates>';
		
		// Exported templates? (to avoid collisions)
		$exported_templates = array();
		
		// Select Templates from DataBase and put them into export
		if ( $this -> appCore -> _POST('style_export_all'))
		{
			// Get style parents
			$style_parents = $this -> appCore -> getStyleParents( $this -> api -> style_id);
			
			// Dont export master style
			unset( $style_parents[1]);
			
			// Export all templates except from master
			$this -> appCore -> db -> query( 'SELECT template_group, template_name, template_code
			FROM ' . DB_PREFIX . 'styles_templates
			WHERE style_id IN (' . join( ',', $style_parents) . ')
			ORDER BY FIELD( style_id,' . join( ',', $style_parents) . ') DESC, template_group, template_name');
		}
		else
		{
			// Export only our templates
			$this -> appCore -> db -> query( 'SELECT template_group, template_name, template_code
			FROM ' . DB_PREFIX . 'styles_templates
			WHERE style_id = \'' . $this -> api -> style_id . '\'
			ORDER BY template_group, template_name');
		}
		
		// Put them into file
		while ( $tpl_result = $this -> appCore -> db -> fetch_array())
		{
			// Template not exported already?
			if ( !isset( $exported_templates[$tpl_result[1]]))
			{
				// Put template on list
				$exported_templates[$tpl_result[1]] = true;
				
				// Add file
				$file_content .= '
		<template>
			<template_group>' . base64_encode( $tpl_result[0]) . '</template_group>
			<template_name>' . base64_encode( $tpl_result[1]) . '</template_name>
			<template_code>' . base64_encode( $tpl_result[2]) . '</template_code>
		</template>';
			}
		}
		
		// Free result
		$this -> appCore -> db -> freeResult();
	
		// Close content
		$file_content .= '
	</style_templates>
</uniboard_style_export>';
		
		// Compress it with GZIP?
		if ( function_exists( 'gzencode'))
		{
			// Compress file with gzip
			$file_content = gzencode( $file_content, 9);
			
			// Send non-compressed XML file
			$this -> appCore -> fileHeaders( 'application/x-compressed', strlen( $file_content), $this -> appCore -> secureFilename( $this -> api -> style_name) . '.xml.gz');
			
			// Echo file content
			exit( $file_content);
		}
		else
		{
			// Send non-compressed XML file
			$this -> appCore -> fileHeaders( 'application/xml', strlen( $file_content), $this -> appCore -> secureFilename( $this -> api -> style_name) . '.xml');
			
			// Echo file content
			exit( $file_content);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws delete style form
	 *
	 */
	
	function drawDeleteForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_style&do=delete&style=' . $this -> api -> style_id));
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'delete', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock();

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['style_form_move_children'], $this -> appCore -> drawList( 'style_replace', $this -> styles, $this -> appCore -> _POST( 'style_replace')), $this -> appCore -> lang['language_form_replace_lang_expl']);
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['delete']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['style_op_delete'], array(
			$this -> appCore -> adminLink( 'act=styles') => $this -> appCore -> lang['acp_styles_list'],
			'' => htmlspecialchars( $this -> api -> style_name)
		), ob_get_clean());
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Deletes style
	 *
	 */
	
	function deleteStyle( $new_parent){
				
		// Create templates groups?
		if ( !USE_GLOB)
		{
			// Templates groups
			$tpl_groups = array();
			
			// Select groups from DB
			$this -> appCore -> db( 'SELECT DISTINC template_group FROM styles_templates');
			
			// Add them to array
			while ( $group_result = $this -> appCore -> db -> fetch_array())
			{
				$tpl_groups[] = $this -> appCore -> secureFilename( $group_result[0]);
			}
			
			// Garbage out
			$this -> appCore -> db -> freeResult();
		}
			
		// New parent is zero?
		if ( $new_parent == 0)
		{
			// Style Children
			$style_children = $this -> appCore -> getStyleChidren( $this -> api -> style_id);
			
			// Add our style
			$style_children[] = $this -> api -> style_id;
			
			// Update user's choice to default style
			$this -> appCore -> db -> update( array( 'user_style' => $this -> appCore -> conf['default_style']), 'users', 'user_style IN (' . join( ',', $style_children) . ')');
			
			// Delete our style from DB
			$this -> appCore -> db -> delete( 'styles', 'style_id IN (' . join( ',', $style_children) . ')');
			$this -> appCore -> db -> delete( 'styles_templates', 'style_id IN (' . join( ',', $style_children) . ')');
			
			// Reiterate styles
			foreach ( $style_children as $style_id)
			{
				// Delete style CSS and JS
				class_files::unlink( STYLES_DATA_PATH . 'style_' . $style_id . '.css');
				class_files::unlink( STYLES_DATA_PATH . 'jscript_' . $style_id . '.js');
				
				// Delete compiled templates
				if ( file_exists( STYLES_DATA_PATH . 'style_' . $style_id . '/index.html'))
				{
					// Kill directory quick way?
					if ( USE_GLOB)
					{
						// Use core's function
						class_files::deleteDir( STYLES_DATA_PATH . 'style_' . $style_id);
					}
					else
					{
						// Delete index file
						class_files::unlink( STYLES_DATA_PATH . 'style_' . $style_id . '/index.html');
				
						// Use group's array
						foreach ( $tpl_groups as $group_name)
						{
							// Delete group?
							class_files::unlink( STYLES_DATA_PATH . 'style_' . $style_id . '/tpl_' . $group_name . PHP_EXT);
						}
						
						// Delete directory
						mkdir( STYLES_DATA_PATH . 'style_' . $style_id);
					}
				}
			}
		}
		else
		{
			// Update user's choice
			$this -> appCore -> db -> update( array( 'user_style' => $new_parent), 'users', 'user_style = \'' . $this -> api -> style_id . '\'');
			
			// Move children styles
			$this -> appCore -> db -> update( array( 'style_parent' => $new_parent), 'styles', 'style_parent = \'' . $this -> api -> style_id . '\'');
						
			// Delete our style from DB
			$this -> appCore -> db -> delete( 'styles', 'style_id = \'' . $this -> api -> style_id . '\'');
			$this -> appCore -> db -> delete( 'styles_templates', 'style_id = \'' . $this -> api -> style_id . '\'');
			
			// Delete style CSS and JS
			class_files::unlink( STYLES_DATA_PATH . 'style_' . $this -> api -> style_id . '.css');
			class_files::unlink( STYLES_DATA_PATH . 'jscript_' . $this -> api -> style_id . '.js');
			
			// Delete compiled templates
			if ( file_exists( STYLES_DATA_PATH . 'style_' . $this -> api -> style_id . '/index.html'))
			{
				// Kill directory quick way?
				if ( USE_GLOB)
				{
					// Use core's function
					class_files::deleteDir( STYLES_DATA_PATH . 'style_' . $this -> api -> style_id);
				}
				else
				{
					// Delete index file
					class_files::unlink( STYLES_DATA_PATH . 'style_' . $this -> api -> style_id . '/index.html');
			
					// Use group's array
					foreach ( $tpl_groups as $group_name)
					{
						// Delete group?
						class_files::unlink( STYLES_DATA_PATH . 'style_' . $this -> api -> style_id . '/tpl_' . $group_name . PHP_EXT);
					}
					
					// Delete directory
					mkdir( STYLES_DATA_PATH . 'style_' . $this -> api -> style_id);
				}
			}
			
		}
		
		// Redirect
		$this -> appCore -> output -> drawRedirect( $this -> appCore -> adminLink( 'act=styles'), $this -> appCore -> lang['acp_style_deleted']);
		
		// Flush cache
		class_cache::flushCache( 'styles');
		
	}
	
}